package com.huike.web.controller.clues;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.service.IClueTrackRecordService;
import com.huike.clues.service.ITbClueService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @Author: Copyright by jiyuzm
 * @Date: 2023/07/13/08:57
 * @Description: 线索跟进
 */

@RestController
@AllArgsConstructor
@Api(tags = "线索跟进相关接口")
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {

    private IClueTrackRecordService clueTrackRecordService;
    private ITbClueService clueService;

    @PostMapping
    @ApiOperation("新增线索跟进记录")
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    public AjaxResult<String> newClueTrackR(@RequestBody ClueTrackRecordVo clueTrackRecord){

        Long clueId = clueTrackRecord.getClueId();
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecord,tbClue);
        tbClue.setId(clueId);
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        int row = clueService.update(tbClue);
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecord,tbClueTrackRecord);
        String username = SecurityUtils.getUsername();
        //将信息添加到线索跟进表
        clueTrackRecordService.newCliueTrackR(tbClue,tbClueTrackRecord,username);
        System.out.println(row);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询线索跟进记录列表")
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    public TableDataInfo<List<ClueTrackRecordVo>> getList(@RequestParam("clueId")Long clueId) {
        List<ClueTrackRecordVo> clueTrackRecordVoList = clueTrackRecordService.getList(clueId);
        return getDataTable(clueTrackRecordVoList);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取线索跟进记录详细信息")
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    public AjaxResult<List<ClueTrackRecordVo>> getById(@PathVariable Long id){
        List<ClueTrackRecordVo> clueTrackRecordVo = clueTrackRecordService.getById(id);
        return AjaxResult.success(clueTrackRecordVo);
    }
}
